﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System.Collections.Generic;

namespace betfairfind
{
    public class BetfairTicks 
    {       
        
        static LinkedList<decimal> ladder = new LinkedList<decimal>();
       
        static BetfairTicks()
        {

            //build ladder
            List<decimal[]> ranges = new List<decimal[]>();
            ranges.Add(new decimal[] { 1, 2, 0.01M });
            ranges.Add(new decimal[] { 2.02M, 3, 0.02M });
            ranges.Add(new decimal[] { 3.05M, 4, 0.05M });
            ranges.Add(new decimal[] { 4.1M, 6, 0.1M });
            ranges.Add(new decimal[] { 6.2M, 10, 0.2M });
            ranges.Add(new decimal[] { 10.5M, 20, 0.5M });
            ranges.Add(new decimal[] { 21, 30, 1 });
            ranges.Add(new decimal[] { 32, 50, 2 });
            ranges.Add(new decimal[] { 55, 100, 5 });
            ranges.Add(new decimal[] { 110, 1000, 10 });

            foreach (var range in ranges)
            {
                decimal current = range[0];
                while (current <= range[1])
                {
                    ladder.AddLast(current);
                    current = current + range[2];
                }
            }
        }


        public static  decimal GetStepsUpFromTick(decimal start, int ticks)
        {
            LinkedListNode<decimal> ret =  ladder.Find(start);
            for (int i = 0; i < ticks; i++)
            {
                ret = ret.Next;
            }
            return ret.Value;
        }

        public static decimal GetStepsDownFromTick(decimal start, int ticks)
        {
            LinkedListNode<decimal> ret = ladder.Find(start);
            for (int i = ticks; i > 0; i--)
            {
                ret = ret.Previous;
            }
            return ret.Value;
        }

        public static bool IsWithinAllowedTickRange(decimal startprice, decimal endprice, int allowedticks)
        {

            // up
            if (startprice < endprice)
            {
                if (endprice <=  GetStepsUpFromTick(startprice, allowedticks)) return true;
            }
            // down
            else
            {
                if (endprice >=  GetStepsDownFromTick(startprice, allowedticks)) return true;
            }

            return false;

        }

    }

}
